<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2016/3/9
 * Time: 22:24
 */
namespace Chealth\Model;

use Think\Model;

class ChealthEnterModel extends Model{

    protected $_validate = array(
        array('aid','require','缺少查体时间id!'),
        array('mobile','require','手机号必填!'),
        array('aid','checkAid','查体时间不存在!', 1, 'callback'),
        array('uid','checkUid','您已预约,只能预约一次!', 1, 'callback'),
        array('name','require','姓名必须填写!'),
        array('department_id','checkDid','部门信息不存在!', 1, 'callback'),
        array('department_id','checkDidEmpty','请选择部门信息!', 1, 'callback'),
//        array('groups_id','checkGid','分组信息不存在!', 1, 'callback'),
        array('chealth_time','checkEnterTime','预约的查体时间不在规定范围内!', 1, 'callback'),
        array('chealth_time','checkEnterTimeNum','该天预约人数已满!', 1, 'callback'),
    );

    protected $_auto = array(
        array('chealth_time','format_time',3,'callback'),
        array('project','auto_project',3,'callback'),
        array('create_time','time',1,'function'),
        array('update_time','time',2,'function'),
        array('status', 1, 1)
    );
    /**
     * 格式化报名时间
     * @param $time
     * @return int
     */
    public function format_time($time){
        if(I('type') == 1){
            return strtotime(date('Y-m-d', $time));
        }
        return $time;
    }

    /**
     * 检查用户是否提交过报名表
     * @param $uid
     * @return bool
     */
    public function checkUid($uid){
        if(I('id') > 0){
            return true;
        }
        $aid = I('aid');
        $info = D('Chealth/ChealthEnter')->where(array('aid'=>$aid,'uid'=>$uid))->find();
        if(empty($info)){
            return true;
        }
        return false;

    }

    /**
     * 检查报名时间是否在规定时间内
     * @param $time
     * @return bool
     */
    public function checkEnterTime($time){
        //获取预约项目信息
        $aid = I('aid');
        $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
        //检查报名时间是否在开始和结束时间内
        if($time < $ainfo['check_start_time'] || $time > $ainfo['check_end_time']){
            var_dump($time > $ainfo['check_end_time']);
            return false;
        }
        //检查报名时间是否是排除时间内
        if(!empty($ainfo['check_exclude_time'])){
            $cetime = $ainfo['check_exclude_time'];
            $len=count($cetime);
            if(I('type') == 1){
                for($i=0;$i<$len;$i+=2){
                    if($time >= $cetime[$i] && $time <= $cetime[$i+1]){
                        return false;
                    }
                }
            }else{
                for($i=0;$i<$len;$i++){
                    if($time == $cetime[$i]){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * 检查这一天预约查体人员是否在规定人数内
     * @param $data
     */
    public function checkEnterTimeNum($time){
        //将时间格式化为精确到天的时间戳,方便比较
//        $time = strtotime(date('Y-m-d', $time));
        //获取预约项目信息
        $aid = I('aid');
        $ainfo = D('Chealth/ChealthTime')->getInfoById($aid);
        //人数限制
        $limit_num = $ainfo['single_day_limit'];
        //获取本次查体的所有报名人员
        $lists = $this->field('id,aid,chealth_time')->where(array('aid'=>$aid))->select();
        if(empty($lists)){
            return true;
        }
        $num = 0;
        foreach($lists as $k => $v){
            if($v['chealth_time'] == $time){
                ++$num;
            }
        }
        if($num >= $limit_num){
            return false;
        }
        return true;
    }

    /**
     * 检查分组信息
     * @param $gid
     * @return bool
     */
    public function checkGid($gid){
        if(empty($gid)){
            return false;
        }
        $info = M('ChealthGroups')->where(array('id'=>$gid))->find();
        if(empty($info)){
            return false;
        }
        return true;
    }

    /**
     * 检查部门id是否正确
     * @param $did
     * @return bool
     */
    public function checkDid($did){
        if(empty($did)){
            return false;
        }
        $info = M('ChealthDepartment')->where(array('id'=>$did))->find();
        if(empty($info)){
            return false;
        }
        return true;
    }

    public function checkDidEmpty($did){
        if($did == 1){
            return false;
        }
        return true;
    }

    /**
     * 检查aid是否存在
     * @param $aid
     * @return bool
     */
    public function checkAid($aid){
        if(empty($aid)){
            return false;
        }
        $info = M('ChealthTime')->where(array('id'=>$aid))->find();
        if(empty($info)){
            return false;
        }
        return true;
    }

    /**
     * 添加或修改报名信息
     * @return bool
     */
    public function updateEnter(){
        if($this->create()){
            if(empty($this->data['id'])){
                $res = $this->add();
            }else{
                $res = $this->save();
            }
            if($res === false){
                $this->error = '写入失败,请重试!';
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }

    /**
     * 管理员添加或修改报名信息
     * @return bool
     */
    public function adminUpdateEnter(){
        $rules = array(
            array('aid','require','缺少查体时间id!'),
            array('mobile','require','手机号必填!'),
            array('aid','checkAid','查体时间不存在!', 1, 'callback'),
            array('uid','checkUid','您已预约,只能预约一次!', 1, 'callback'),
            array('name','require','姓名必须填写!'),
            array('department_id','checkDid','部门信息不存在!', 1, 'callback'),
            array('department_id','checkDidEmpty','请选择部门信息!', 1, 'callback'),
            array('chealth_time','checkEnterTime','预约的查体时间不在规定范围内!', 1, 'callback'),
        );
        if($this->validate($rules)->create()){
            if(empty($this->data['id'])){
                $res = $this->add();
            }else{
                $res = $this->save();
            }
            if($res === false){
                $this->error = '写入失败,请重试!';
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }

    /* 自动完成函数 */
    public function auto_project($data){
        if(empty($data) || !is_array($data)){
            return $data;
        }
        return implode(',', $data);
    }
}